package chapter03

import org.apache.log4j.{Level, Logger}
import org.apache.spark.{SparkConf, SparkContext}

object Test34_temperature {
  def main(args: Array[String]): Unit = {
    Logger.getLogger("org.apache.spark").setLevel(Level.WARN)
    val conf = new SparkConf().setMaster("local[*]").setAppName("temperature")
    val sc = new SparkContext(conf)
    //读取文件
    val value = sc.textFile("input/avgTemperature.txt")
    val value1 = value
      .map(e => e.split("\t"))
      .map(e=>(e(0),e(1).toDouble))
    println(value1.collect().toList)
    //计算每个城市的最高温度和最低温度
    val value2 = value1
      .groupBy(e=>e._1)
      .mapValues(e => (e.max, e.min))
      .map(e=>e._2)
    println(value2.collect().toList)
    //按照最高温度对城市排序
    val value3 = value1.groupBy(e => e._1)
      .mapValues(e=>e.toList)
      .mapValues(e=>e.map(f=>f._2).max)
      .sortBy(e=>e._2,false)
    println(value3.collect().toList)
    //计算每个城市的平均气温
    println(value.map(e=>e.split("\t").toList)
      .groupBy(e=> e.head)
      .mapValues(e=>e.map(f=>f(1).toFloat))
      .mapValues(e=>e.sum/e.size)
      .sortBy(e=>e._2,ascending = false)
    .collect().toList)
  }
}
